//
//  ZHHttpDownLoadTools.swift
//  ZHWeibo
//
//  Created by user on 15/12/6.
//  Copyright © 2015年 heima. All rights reserved.
//

import UIKit
import AFNetworking
//定义请求方法枚举
enum ZHRequestMethod:String {
    case GET = "GET"
    case POST = "POST"
}

class ZHHttpDownLoadTools: AFHTTPSessionManager {
    let WB_APPKEY = "4172081718"
    let WB_APP_SECRET = "6ff9179b059b49b53e512e4706235951"
    let WB_REDIRECT_URI = "http://www.sina.com.cn"
    //创建单例----就是懒加载
    
    static let shareInstance:ZHHttpDownLoadTools = {
        let tools = ZHHttpDownLoadTools()
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tools
    }()//加()执行------519273266,18513174598,
    //单例的简便写法
    static let shareInstance1:ZHHttpDownLoadTools = ZHHttpDownLoadTools()

    //自定义一个闭包
    typealias ZHNetworkToolsCallBack = (response: AnyObject?, error: NSError?)->()
    
    
    //提供一个方法,共外界访问,提供下载
    
    func getNetworkData(Method:ZHRequestMethod, urlString: String,parameters: AnyObject?, callback:(response:AnyObject?, error:NSError?)->()){
        //定义闭包Block
        let successBlock = {(dataTask:NSURLSessionDataTask?, response: AnyObject) -> Void in
            callback(response: response, error: nil)
        }
        let failBlock = {(dataTask:NSURLSessionDataTask?, error: NSError) -> Void in
            callback(response: nil, error: error)
        }
        //调用AFN中的方法
        if Method == .GET {
            self.GET(urlString, parameters: parameters, success: successBlock, failure: failBlock)
        }
        else{
            self.POST(urlString, parameters: parameters, success: successBlock, failure: failBlock)
            print(parameters)
        }
    }
}


extension ZHHttpDownLoadTools{
    //加载最新的朋友圈微博
    func getFrendsWeiInfo(accessToken: String, callBack: ZHNetworkToolsCallBack) {
        let urlString = "https://api.weibo.com/2/statuses/friends_timeline.json"
        /// 这里如果写成AnyObject就会有问题,还有一点就是这里面[]不能出现不确定的值,但是param可以不确定,但是里面的值不能够不确定
        let params = [
            "access_token": accessToken
        ]
        //发送请求
        getNetworkData(ZHRequestMethod.GET, urlString: urlString, parameters: params, callback: callBack)
    }
}



// MARK: - 登录相关的请求
extension ZHHttpDownLoadTools{
    // 加载accessToken,并且把加载完成之后的数据回调回去
    func getAccessToken(code: String, callback: ZHNetworkToolsCallBack){
        
        // 请求地址
        let urlString = "https://api.weibo.com/oauth2/access_token"
        // 请求参数
        let params = [
            "client_id": WB_APPKEY,
            "client_secret": WB_APP_SECRET,
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": WB_REDIRECT_URI
        ]
        // 发送请求
        getNetworkData(ZHRequestMethod.POST, urlString: urlString, parameters: params, callback: callback)
    }
    
    func getUserInfo(uid: String, accessToken: String, callback: ZHNetworkToolsCallBack){
        //接口
        let urlString = "https://api.weibo.com/2/users/show.json"
        //参数
        let params = [
            "access_token": accessToken,
            "uid": uid
        ]
        // 发送请求
        getNetworkData(ZHRequestMethod.GET, urlString: urlString, parameters: params, callback: callback)
            
    }
}






















